Skip to content

fix(agentchat): make TokenUsageTermination component config loadable when a token limit is unset - #8426

Open
DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:codex/tokenusage-config-roundtrip-20261103
Open

DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:codex/tokenusage-config-roundtrip-20261103

Conversation

@DZH166

@DZH166 DZH166 commented Nov 3, 2026

Copy link
Copy Markdown

Why are these changes needed?

TokenUsageTermination cannot survive a declarative dump_component() / load_component() roundtrip unless all three token limits are set. For example:

term = TokenUsageTermination(max_total_token=100)
config = term.dump_component()          # config == {'max_total_token': 100}
TokenUsageTermination.load_component(config)
# pydantic ValidationError:
#   max_prompt_token    Field required
#   max_completion_token Field required

Root cause: dump_component() serializes the config with model_dump(exclude_none=True), so any limit left as None is omitted from the dumped dictionary — but TokenUsageTerminationConfig declared max_total_token, max_prompt_token, and max_completion_token as required fields (no default). Since the constructor allows omitting any subset of the limits (only "at least one" is required), the common single-limit usage produces a component config that can never be loaded back. Nested inside Or/And termination conditions it fails the same way, so saved teams using TokenUsageTermination with a partial limit are not restorable.

Fix: give the three config fields = None defaults, matching the constructor signature (max_total_token: int | None = None, etc.). Omitted fields now validate back to None, and the constructor's "at least one limit required" check continues to reject genuinely invalid (all-None) configs. Previously dumped configs that omitted unset limits become loadable again.

Related issue number

Self-found bug (no existing issue); repro and root-cause analysis included above.

Checks

  • I've included any doc changes needed for https://microsoft.github.io/autogen/ (no doc changes needed: config schema behavior now matches its constructor and docstring).
  • I've added tests (if relevant) corresponding to the changes introduced in this PR. — test_token_usage_termination_component_roundtrip in python/packages/autogen-agentchat/tests/test_termination_condition.py; red before the fix (ValidationError), green after (14/14 in the file).
  • I've made sure all auto checks have passed. — ruff check / ruff format --check clean on both changed files; git diff --check clean; tests/test_declarative_components.py still passes.

Validation

  • Before: python -m pytest tests/test_termination_condition.py → 1 failed, 13 passed (the new roundtrip test fails with pydantic ValidationError: Field required from ComponentLoader.load_component).
  • After: same command → 14 passed.
  • Combination probe: all 7 subsets of {max_total_token, max_prompt_token, max_completion_token} now roundtrip (pre-fix: 7 of 8 combos failed, including nested OrTerminationCondition configs).

AI assistance

This contribution was prepared with AI assistance (code localization, patch drafting, and test authoring), reviewed and validated by the submitter. The bug was identified and the fix verified locally against the repository's own test suite.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant